package com.ruoyi.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.OrderUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.workflow.service.IProcessInstanceService;
import com.ruoyi.workflow.utils.WorkFlowUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.demo.domain.bo.BsTripsBo;
import com.ruoyi.demo.domain.vo.BsTripsVo;
import com.ruoyi.demo.domain.BsTrips;
import com.ruoyi.demo.mapper.BsTripsMapper;
import com.ruoyi.demo.service.IBsTripsService;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 出差信息Service业务层处理
 *
 * @author lgp
 * @date 2022-07-29
 */
@RequiredArgsConstructor
@Service
public class BsTripsServiceImpl implements IBsTripsService {

    private final BsTripsMapper baseMapper;

    @Autowired
    private WorkFlowUtils workFlowUtils;

    @Autowired
    private IProcessInstanceService iProcessInstanceService;

    @Autowired
    private OrderUtil orderUtil;

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");

    /**
     * 查询出差信息
     */
    @Override
    public BsTripsVo queryById(String id){
        BsTripsVo vo = baseMapper.selectVoById(id);
        vo.setProcessInstanceId(iProcessInstanceService.getProcessInstanceId(id));
        workFlowUtils.setStatusFileValue(vo, Arrays.asList(vo.getId()),vo.getId());
        return vo;
    }

    /**
     * 查询出差信息列表
     */
    @Override
    public TableDataInfo<BsTripsVo> queryPageList(BsTripsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BsTrips> lqw = buildQueryWrapper(bo);
        Page<BsTripsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        List<BsTripsVo> records = result.getRecords();
        if(CollectionUtil.isNotEmpty(records)){
            List<String> collectIds = records.stream().map(BsTripsVo::getId).collect(Collectors.toList());
            for (BsTripsVo record : records) {
                workFlowUtils.setStatusFileValue(record,collectIds,record.getId());
                workFlowUtils.setProcessInstIdFileValue(record,collectIds,record.getId());
            }
        }
        result.setRecords(records);
        return TableDataInfo.build(result);
    }

    /**
     * 查询出差信息列表
     */
    @Override
    public List<BsTripsVo> queryList(BsTripsBo bo) {
        LambdaQueryWrapper<BsTrips> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BsTrips> buildQueryWrapper(BsTripsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BsTrips> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTripcode()), BsTrips::getTripcode, bo.getTripcode());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BsTrips::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), BsTrips::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getTriper()), BsTrips::getTriper, bo.getTriper());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BsTrips::getAddress, bo.getAddress());
        lqw.eq(bo.getDays() != null, BsTrips::getDays, bo.getDays());
        lqw.eq(bo.getStartTime() != null, BsTrips::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, BsTrips::getEndTime, bo.getEndTime());
        lqw.eq(bo.getStatus() != null, BsTrips::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增出差信息
     */
    @Override
    public BsTrips insertByBo(BsTripsBo bo) {
        BsTrips add = BeanUtil.toBean(bo, BsTrips.class);
        String date = simpleDateFormat.format(new Date());
        add.setTripcode(orderUtil.getOrderNoByAtomic("TRP","TRP:CODE:"+date));
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setUserId(String.valueOf(loginUser.getUserId()));
        add.setDeptId(String.valueOf(loginUser.getDeptId()));
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return add;
    }

    /**
     * 修改出差信息
     */
    @Override
    public BsTrips updateByBo(BsTripsBo bo) {
        BsTrips update = BeanUtil.toBean(bo, BsTrips.class);
        validEntityBeforeSave(update);
        baseMapper.updateById(update);
        return update;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsTrips entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出差信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
